/*******************************************************************************
 * @project: aohuproject
 * @package: com.aohua.px
 * @file: SpecificationAutoGenerateAction.java
 * @author: fionn
 * @created: 2015年8月13日
 * @purpose:
 * 
 * @version: 1.0
 * 
 * 
 * Copyright 2015 HAND All rights reserved.
 ******************************************************************************/
package com.aohua.px;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import org.apache.log4j.Logger;

import com.agile.api.APIException;
import com.agile.api.IAgileSession;
import com.agile.api.ICell;
import com.agile.api.IDataObject;
import com.agile.api.IItem;
import com.agile.api.INode;
import com.agile.api.ItemConstants;
import com.agile.px.ActionResult;
import com.agile.px.ICustomAction;
import com.hand.agile.utils.CellUtils;

/**
 * @author fionn
 *
 */
public class SpecificationAutoGenerateAction implements ICustomAction {

    private static Logger log = Logger.getLogger(SpecificationAutoGenerateAction.class);
    private Properties config;
    private String hyphen;
    
    /**
     * 
     */
    public SpecificationAutoGenerateAction() {
        
    }

    /* (non-Javadoc)
     * @see com.agile.px.ICustomAction#doAction(com.agile.api.IAgileSession, com.agile.api.INode, com.agile.api.IDataObject)
     */
    @Override
    public ActionResult doAction(IAgileSession session, INode arg1,
            IDataObject arg2) {
        ActionResult result = null;
        try {
            session.disableAllWarnings();
            IItem item = (IItem)arg2;
            generateSpecification(item);
            result = new ActionResult(ActionResult.STRING, "自动生成规格型号成功.");
        } catch (Exception e) {
            result = new ActionResult(ActionResult.EXCEPTION, e);
            log.error(e);
            e.printStackTrace(System.out);
        } finally {
            try {
                session.enableAllWarnings();
            } catch (APIException e) {
                log.error(e);
            }
        }
        return result;
    }

    private void generateSpecification(IItem item) throws APIException {
        initConfig();
        ICell cell = item.getCell(config.getProperty("attributeFlaged"));
        String type = CellUtils.getCellStringValue(cell);
        String attrstr = config.getProperty(type);
        if(attrstr == null || "".equals(attrstr)) {
            throw new APIException(new Exception(type + " 未配置自动规格型号组合规则。"));
        }
        StringBuilder sb = new StringBuilder();
        String[] apis = attrstr.split(",");
        for(int i = 0; i<(apis.length -1) ; i++) {
            cell = item.getCell(apis[i]);
            sb.append(cell.getAttribute().getName()).append(":").append(CellUtils.getCellStringValue(cell)).append(hyphen);
        }
        cell = item.getCell(apis[apis.length - 1]);
        sb.append(cell.getAttribute().getName()).append(":").append(CellUtils.getCellStringValue(cell));
        item.setValue(ItemConstants.ATT_PAGE_TWO_MULTITEXT31, sb.toString()); 
    }
    
    private void initConfig() {
        InputStream in = null;
        try {
            in  = SpecificationAutoGenerateAction.class.getClassLoader().getResourceAsStream("/autodescription.xml");
            config = new Properties();
            config.loadFromXML(in);
            this.hyphen = config.getProperty("hyphen");
        } catch (Exception e) {
            log.error(e);
            e.printStackTrace(System.out);
        } finally {
            if(in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace(System.out);
                }
            }
        }
    }
}
